#ifndef _BOOK_NAME_HPP_
#define _BOOK_NAME_HPP_

#include <ext/hash_map>
#include <string>
#include <cstdio>


class BookName
{
    __gnu_cxx::hash_map<uint32_t, std::string> _name_map;


public:
    BookName()
    {
    }


    std::string GetName(uint32_t id) 
    {
        __gnu_cxx::hash_map<uint32_t, std::string>::const_iterator it = 
            _name_map.find(id);

        if (it == _name_map.end()) {
            return "";
        } else
            return it->second;
    }

    int Read(const char *file_name)
    {
        FILE *f = fopen(file_name, "rt");

        if (f == NULL)
            return -1;

        char line[128];
        while(fgets(line, 128, f)) {
            uint32_t id = 0;
            bool is_num = true;
            int name_pos = 0;

            for(int i = 0; i < 128; i++) {
                if (line[i] == ' ') {
                    if (is_num == true) {
                        name_pos = i + 1;
                    }
                    is_num = false;
                } else if (line[i] == '\n') {
                    line[i] = 0;
                    break;
                }

                if (is_num) 
                    id = id * 10 + line[i] - '0';
            }

            _name_map[id] = std::string(&line[name_pos]);
        }
        fclose(f);
    }

};

#endif

